<%@ page import="edu.sctu.User" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.DriverManager" %><%--
  Created by IntelliJ IDEA.
  edu.sctu.User: Administrator
  Date: 2018/4/17
  Time: 16:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>判断</title>
</head>
<body>

<%
String username = request.getParameter("username");
String password = request.getParameter("password");


//数据库中读取数据保存在List集合中
List<User> userList=new ArrayList<User>();

    //3+1读取数据库数据保存到List中
    Connection connection=null;
    Statement statement=null;
    ResultSet resultSet=null;

    String sql="select * from t_users";

    Class.forName("com.mysql.jdbc.Driver");


    connection= DriverManager.getConnection(
    "jdbc:mysql://localhost:3306/sctu_javaweb_2018?serverTimezone=UTC",
    "root",
    "123456"
    );

    statement=connection.createStatement();
    resultSet=statement.executeQuery(sql);

    while(resultSet.next()){
    int id=resultSet.getInt(1);
    String name=resultSet.getString(2);
    String password0=resultSet.getString(3);

    User user = new User();

    user.setId(id);
    user.setName(name);
    user.setPassword(password0);

    userList.add(user);
    }


    resultSet.close();
    statement.close();
    connection.close();

    request.setAttribute("userList", userList);

    for(int i = 0; i<userList.size(); i++) {
     if(userList.get(i).getName().equals(username) && userList.get(i).getPassword().equals(password)) {

       out.write("登录成功!");
       boolean isLogin = true;
       session.setAttribute("isLogin", "true");
       response.sendRedirect("userList.jsp");
      }
    }
    out.write("<p>用户名或密码错误。</p>");
    out.write("<meta http-equiv=\"refresh\" content=\"0.5;URL=http://localhost:8080/login.jsp\">");
 %>


<%--<%--%>
    <%--String username = request.getParameter("username");--%>
    <%--String password = request.getParameter("password");--%>
    <%--if ("Admin".equals(username) &&--%>
            <%--"123456".equals(password)) {--%>
        <%--//管理员登录--%>
        <%--out.write("登录成功！");--%>
        <%--boolean isLogin = true;--%>
        <%--session.setAttribute("isLogin", "true");--%>
        <%--response.sendRedirect("userList.jsp");--%>
    <%--} else {--%>
        <%--out.write("<p>用户名或密码错误。</p>");--%>
    <%--}--%>
<%--%>--%>


</body>
</html>
